var scroller = function(params){
	
	var that = this;
	var widget = null;
	var id = null;

	var data = null;

	//flags
	var initialLoad = true; 

	//EVENT_NAMES
	var events = {
		ON_LOAD: "org.jscourse.scroller.load",
		ON_LOAD_START: "org.jscourse.scroller.load_start",
		ON_SCROLL: "org.jscourse.scroller.on_scroll",
	}
	 


	var _default = {
		id: null,
		widget: document.body, //Will hold the HTML Element its linked to
		query: null,
		url: null,
		method: "GET",
		bypassSOP: false,

		serviceHandler: getServieHandler()
	}



	params = extend(_default, params);

	//***********************************************************
	function initialize(){

		id = (params.id != null && params.id!= undefined) ? params.id: that.generateId();
		widget = params.widget;


		params.serviceHandler.initialize({
				success: that.handleResponse,
				url: params.url,
				method: params.method,
				request: {p: params.query},
				async: false
			});	


		params.serviceHandler.call();
		initialLoad = false;


	}
	//Picks service Handler based on the bypassSOP value
	function getServieHandler(){
		var handler = null;

		if( params.bypassSOP){
			handler = new jsonp();
		}else{
			handler = new ajax();
		}


		return handler;
	}
	//***********************************************************
	this.handleResponse = function(response, xhrObject){
		
		if(typeof response ==  "object"){
			parseData(response);
		}else{
			throw new Error("Cannot parse data, not a valid JSON object: " + response);
		}

		data = response;

		console.log(response);
	}

	//***********************************************************
	function parseData(data){
		var list="";

		that.createInfoBar();

		for(postIndex in data.results){

			post = data.results[postIndex];

			list ='<div class="post">' +
					'<img src="'+ post.profile_image_url +'" alt="'+ post.from_user +'"/>'+
					'<h5>'+ post.from_user_name +' ('+ post.from_user +') posted on '+ post.created_at +'</h5>'+
					'<p>'+
						post.text +
					'</p>'+
				'</div>';

			widget.innerHTML += list;
		}
		
				
		that.createToolbar();

	}
	//***********************************************************
	this.createInfoBar = function(data){
		if(initialLoad ){


		}
	}
	//***********************************************************
	this.createToolbar = function(){
		if(initialLoad ){

			var toolBar = document.createElement("div");

			if(widget != document.body){
				widget.parentElement.appendChild(toolBar);
			}else{
				widget.appendChild(toolBar);
			}

			toolBar.innerHTML = '<input id="'+ id+'_nextButton" type="button" value="next"/>';
			document.getElementById( id+"_nextButton").addEventListener('click', that.loadNext);
		}		
	}

	//***********************************************************
	this.loadNext = function(event){
		params.serviceHandler.call(undefined, data.next_page, undefined );
	}


	this.generateId = function(){
		var d = new Date();
		return d.getMilliseconds();
	}

	initialize();


	return this;
}

